package vip.youngboy.stompdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

@Controller
public class StompSequence {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/sequence")
    public void sequence(){
        int index=0;
        while(index<200){
            //用于向客户端发送前200个数字，我们希望树循序接收到0到199这200个数字
            simpMessagingTemplate.convertAndSend("/topic/sequence",index++);
            try {
                //我们睡眠1毫秒，1毫秒对于发送数据已经很大了。CPU执行都是纳米级的
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


}
